const del = require("del");
const gulp = require("gulp");
const gutil = require("gulp-util");
const webserver = require("gulp-webserver");
const replace = require('gulp-replace');
const rename = require("gulp-rename");

// 环境变量
process.env.MODE = 'all'

// 获取用户配置
const config = require("./gulp.config.js");
const unionConfig = require("./gulp.union.config.js");

// 日志输出
function log() {
  const args = Array.prototype.slice.call(arguments);
  gutil.log.apply(null, args);
}

// 复制文件
function copyFile(files, output, entry) {
  if (files && files.length) {
    return gulp
      .src(files, { allowEmpty: true, base: entry || config.entry })
      .pipe(gulp.dest(output));
  } else {
    return log("请配置gulp.config.js内容~");
  }
}

async function copy() {
  console.log(process.env.MODE)
  const conf = process.env.MODE === 'all' ? config : unionConfig

  log(`将${conf.entry}目录中通用文件同步到[${conf.output}]目录中...`);
  return conf.output.forEach(item => {
    const filters = []
    conf.filters.forEach(row => {
      if (typeof row === 'object') {
        // 过滤指定项目
        if (row.output.includes(item)) {
          filters.push(`!${conf.entry}${row.value}`)
        }
      } else {
        // 全部过滤
        filters.push(`!${conf.entry}${row}`)
      }
    })
    const files = [...conf.files.map(item => conf.entry + item), ...filters]
    // console.log(files)
    return copyFile(files, item, conf.entry);
  })
}

// clean
function clean() {
  return del(['dist'])
}

// 启动本地站点
function server() {
  return gulp.src("fm-mm/dist").pipe(
    webserver({
      host: "0.0.0.0",
      port: 9007,
      livereload: true,
      fallback: "index.html",
      proxies: [
        {
          source: "/api",
          target: "http://api.madaomall.com",
        },
      ],
    })
  );
}

async function setEnvUnion() {
  return process.env.MODE = 'union'
}

// 99猫
async function change99Env() {
  return gulp.src([
    'ms-customer/.env.99',
    'ms-operator/.env.99'
  ], { base: process.cwd() })
    .pipe(rename({
      extname: ''
    }))
    .pipe(gulp.dest(process.cwd()))
}
async function change99Vite() {
  return gulp.src([
    'ms-customer/vite.config.ts',
    'ms-operator/vite.config.ts'
  ], { base: process.cwd() })
    .pipe(replace(/target:\s.*?,/gi, () => {
      return `target: 'https://testbms.itaored.com/api/',`
    }))
    .pipe(gulp.dest(process.cwd()))
}

// 和气
async function changeHQEnv() {
  return gulp.src([
    'ms-customer/.env.hqzx',
    'ms-operator/.env.hqzx'
  ], { base: process.cwd() })
    .pipe(rename({
      extname: ''
    }))
    .pipe(gulp.dest(process.cwd()))
}
async function changeHQVite() {
  return gulp.src([
    'ms-customer/vite.config.ts',
    'ms-operator/vite.config.ts'
  ], { base: process.cwd() })
    .pipe(replace(/target:\s.*?,/gi, () => {
      return `target: 'https://testwx.itaored.com/api/',`
    }))
    .pipe(gulp.dest(process.cwd()))
}

// 和气SAAS
async function changeSaaSEnv() {
  return gulp.src([
    'ms-customer/.env.saas',
    'ms-operator/.env.saas'
  ], { base: process.cwd() })
    .pipe(rename({
      extname: ''
    }))
    .pipe(gulp.dest(process.cwd()))
}
async function changeSaaSVite() {
  return gulp.src([
    'ms-customer/vite.config.ts',
    'ms-operator/vite.config.ts'
  ], { base: process.cwd() })
    .pipe(replace(/target:\s.*?,/gi, () => {
      return `target: 'https://testhq.itaored.com/api/',`
    }))
    .pipe(gulp.dest(process.cwd()))
}

// 复制文件
gulp.task("copy:all", gulp.series(copy));

// 复制文件
// gulp.task("copy:union", gulp.series(setEnvUnion, copy));
// 复制文件
gulp.task("change:99", gulp.series(clean, change99Env, change99Vite));
gulp.task("change:HQ", gulp.series(clean, changeHQEnv, changeHQVite));
// gulp.task("change:SAAS", gulp.series(clean, changeSaaSEnv, changeSaaSVite));

// server
// gulp.task("server", gulp.series(server));
